﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Custom;
using BizInterface.HR;

namespace Presentation.Web.Pages.Employee
{
    public partial class EmployeeEdit : System.Web.UI.Page
    {
        public IEmployeeService EmployeeService
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadEmployee();
            }

        }

        private void LoadEmployee()
        {
            string employeeID = Request.QueryString[Constants.Web.ID];
            if (string.IsNullOrEmpty(employeeID))
            {
                this.lblHeadTitle.Text = "Add Employee";
            }
            else
            {
                this.lblHeadTitle.Text = "Update Employee";
                this.hidEmployeeId.Value = employeeID;
                int id = Convert.ToInt32(employeeID);
                Model.Employee employee = EmployeeService.GetEmployee(id);
                if (employee != null)
                {                    
                    this.txtName.Text = employee.Name;
                    this.txtDescription.Text = employee.Description;
                }
                else
                {
                    GotoList();
                }
            }
        }

        private void GotoList()
        {
            Response.Redirect("EmployeeList.aspx");
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            GotoList();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                try
                {
                    Model.Employee employee = null;

                    if (String.IsNullOrEmpty(this.hidEmployeeId.Value))
                    {
                        employee = new Model.Employee();
                    }
                    else
                    {
                        int id = Convert.ToInt32(this.hidEmployeeId.Value);
                        employee = EmployeeService.GetEmployee(id);
                    }

                    employee.Name = this.txtName.Text; // this.Server.HtmlEncode(this.txtName.Text);
                    employee.Description = this.txtDescription.Text;

                    EmployeeService.SaveEmployee(employee);

                }
                catch (Exception exception) { }
            }

            GotoList();
        }
    }
}